<?php 
require_once('config/config.php');
require_once('checked.php'); //判断是否登录   


// 接受js中ajax
$action = isset($_POST['action']) ? trim($_POST['action']) :'';
//说明是ajax的post请求
if ($action == 'ajax') {
  // 返回的值
  $success = [
    'result'=>false,
    'msg'=>''
  ];
  //获取手机号码的值
  $mobile = isset($_POST['mobile']) ? trim($_POST['mobile']) :'';
  if(empty($mobile)){
      $success['msg'] = '手机号码为空';
      echo json_encode($success);
      exit;
  }
  // 判断手机号是否为空
  $sql = "SELECT * FROM {$pre_}person WHERE mobile = '$mobile'";
  $check = select($sql);
  if(!$check){
      $success["msg"] = "没有重复手机号吗，不可以添加!";
      $success['result'] = true;
      echo json_encode($success);
      exit;
  }else{
    $success["msg"] = "手机号码重复!";
    $success['result'] = false;
    echo json_encode($success);
    exit;
  }


}


//判断是不是post请求
if($_POST){
  // 获取表单提交的值
  $nickname = isset($_POST['nickname']) ? trim($_POST['nickname']) :'';
  $mobile = isset($_POST['mobile']) ? trim($_POST['mobile']) :'';
  $email = isset($_POST['email']) ? trim($_POST['email']) :'';
  $address = isset($_POST['address']) ? trim($_POST['address']) :'';
  $createtime = isset($_POST['createtime']) ? trim($_POST['createtime']) :  '';
  $depid = isset($_POST['depid']) ? trim($_POST['depid']) : 0;
  $jobid = isset($_POST['jobid']) ? trim($_POST['jobid']) : 0;

  //php中再次判断手机号的问题
  //判断手机号是否为空
  $sql = "SELECT * FROM {$pre_}person WHERE mobile = '$mobile'";
  $check = select($sql);
  if($check){
    notice("手机号码已经存在!",'PersonAdd.php');
    exit;
  }
  // 判断入职时间
  if(empty($createtime)){
    $createtime = time();
  }else{
    // 将字符串转化成标准时间
    $createtime = strtotime($createtime);
  }
  // 封装数据
  $data = [
    "nickname"=> $nickname,
    "mobile"=> $mobile,
    "email"=> $email,
    "address"=> $address,
    "createtime"=> $createtime,
    "depid"=> $depid,
    "jobid"=> $jobid
  ];
  //上传头像的功能
  if(isset($_FILES['avatar'])&&$_FILES['avatar']['size']>0)
  {
    $res = UploadFile('avatar','assets/ploded');
    if($res['result']){
      //刷新跳转 
      $data['avatar'] = $res['data'];
    }
    else{
      notice($res['msg']);
      exit;
    }
  }
  //给表名
  $table = 'person';

  //书写添加的方法
  $i =  insert($table,$data);
  if($i){
    notice('添加员工信息成功!','PersonList.php');
    exit;
  }else{
    notice('添加员工信息失败!');
    exit;
  }

}
//sql 语句进行查询 部门和职务表
$sql1 = "SELECT * FROM {$pre_}department";
$depatList = selectAll($sql1);

//查询多条数据
$sql2 = "SELECT * FROM {$pre_}job";
$jobList = selectAll($sql2);
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <?php require_once('meta.php') ?>
  </head>
  
  <body>
    <!-- 头部 -->
    <?php require_once('header.php') ?>
    
    <div class="sidebar-nav">
        <a href="#dashboard-menu" class="nav-header" data-toggle="collapse"><i class="icon-dashboard"></i>控制面板</a>
        <ul id="dashboard-menu" class="nav nav-list collapse in">
            <li><a href="index.php">首页</a></li>
            <li><a href="list.php">员工管理</a></li>
        </ul>
    </div>

    <div class="content">
        <div class="header">
            <h1 class="page-title">员工添加</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                    
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='PersonList.php'"><i class="icon-list"></i> 员工列表</button>
                  <div class="btn-group">
                  </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                      <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">  
                            <label>员工姓名</label>
                            <input type="text" name="nickname" value="" placeholder="请输入姓名" class="input-xxlarge" required>
                            <label>手机号码</label>
                            <input type="text" name="mobile" id="mobile" value="" placeholder="请输入手机号码" class="input-xxlarge" required>
                            <span id="notice" style="display:none;color:red;font-size:16px;"></span>
                            <label>邮箱</label>
                            <input type="text" name="email" value="" placeholder="请输入邮箱" class="input-xxlarge" required>
                            <label>地址</label>
                            <input type="text" name="address" value="" placeholder="请输入地址" class="input-xxlarge" required>
                            <label>入职时间</label>
                            <input type="date" name="createtime" value="<?php echo date("Y-m-d",time()) ?>" class="input-xxlarge">
                            <label>部门分类</label>
                            <select name="depid" class="input-xlarge" required>
                            <?php foreach($depatList as $value){ ?>
                              <option value="<?php echo $value['id']; ?>"><?php echo $value['name'];?></option>
                              <?php }?>
                            </select>
                            <label>职务分类</label>
                            <select name="jobid" class="input-xlarge" required>
                              <?php foreach($jobList as $value){ ?>
                              <option value="<?php echo $value['id']; ?>"><?php echo $value['name'];?></option>
                              <?php }?>
                            </select>
                            <label>头像</label>
                            <a href="javascript:void(0)" style="display:inline-block;width:150px;height:150px;" id="preview" onclick="avatar.click()">
                              <img src="./assets/ploded/preview1.gif" style="width: 100%;height:100%;object-fit:cover"  />
                            </a>
                            
                            <input type="file" id="avatar" name="avatar" class="input-xxlarge" style="display:none">
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交" />
                        </form>
                      </div>
                  </div>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                  </div>
                  <div class="modal-body">
                    
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                  </div>
                  <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                  </div>
                </div>
                
                <footer>
                    <hr>
                    <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                </footer>
                    
            </div>
        </div>
    </div>
  </body>
</html>
<script>
    //手机号码 ajax
    $("input[name=mobile]").change(function(){
      var mobile = $.trim($(this).val());
      //手机号码为空的情况下
      if(!mobile){
          $("#notice").css({"display":"inline-block"});
          $("#notice").html("手机号码不能为空!");
          return false;
      }
     
      //手机号码的正则表达式
      var reg = /^1[3-9]\d{9}$/
      // 没通过
      if(!reg.test(mobile)){
        console.log(!reg.test(mobile))
        $("#notice").css({"display":"inline-block"});
        $("#notice").html("手机号码格式不正确");
        return false;
      }
      // 封装data数据
      var data = {
        action:'ajax',
        mobile:mobile
      }
      //如果手机号码输入正确 ，发送ajax 请求
      $.ajax({
        type:'POST',
        data:data,
        dataType:'json',
        // 内部是回调函数 将数据发送到php后端
        success:(success)=>{
          if(success.result){
            $("#notice").css({
              "display":"inline-block",
              "color":"green"
            });
            $("#notice").html(success.msg);
            return false;
          }else{
            $("#notice").css({"display":"inline-block"});
            $("#notice").html(success.msg);
            return false;
          }
        },
        error:(error)=>{
          console.log(error)
        }
      })
    })

    // 头像预览
    $("#avatar").change(function(){
        var file = this.files[0]?this.files[0]:null;
        if(!file){
          return false;
        }

        //文件需要文件读取器去读取
        var reader = new FileReader();
        //给url路径
        reader.readAsDataURL(file);

        // 给文件一个加载的事件
        reader.onload =function(e){
          $("#preview img").attr('src',e.target.result);
        }
    })
</script>